﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Find1stNonRepeated
{
    class Program
    {
        static int FindNonRepeated(char [] czParam)
        {

            if (czParam.Length <= 0)
            {
                Console.WriteLine("Please input an valid string!");
                return -1;
            }
            

            int nIndex = 0;
            do 
            {
                char cCurPos = czParam[nIndex];
                if (cCurPos.Equals('\0'))
                {
                    nIndex++;
                    continue;
                }
                bool bDuplicate = false;
                for (int n = nIndex + 1; n <  czParam.Length; n++)
                {
                    if (czParam[n].Equals('\0'))
                    {
                        continue;
                    }
                    if(cCurPos.Equals(czParam[n]))
                    {
                        czParam[n] ='\0';
                        bDuplicate = true;
                    }
                }

                if (bDuplicate)
                    czParam[nIndex] = '\0';
                else
                    return nIndex;

                nIndex++;
  
            } while (nIndex < czParam.Length);

            return -1;

        }

        static void Main(string[] args)
        {
            string strTest = string.Empty;
            do 
            {
                Console.WriteLine("Please input a string as test case:");
                strTest = Console.ReadLine();
               
                //Console.WriteLine("Start Find the first non-repeated character in input string " + Environment.NewLine + "{0}", strTest);
                int nPos = FindNonRepeated(strTest.ToCharArray());
                if (nPos != -1)
                {
                    Console.WriteLine("Find the first non-repeated character \" {0} \"in {1}", strTest[nPos], nPos); 
                }
                
            } while (strTest.ToLower() != "exit");


        }
    }
}
